var defaultsTitle='',
	defaultsDesc='',
	defaultsImgUrl='',
	defaultsLink=window.location.href;
var Indiana={
	option:{
		isAll:false,
		start:20,
		limit:20,
		count:0
	},
	getUrl:function(type,module,method,params){
		
		var  _params="params="+encodeURIComponent(JSON.stringify(params)),
			customerId=$.util.getParam('customerId'),
			_url="call."+type+"?customerId="+customerId+"&module="+module+"&method="+method+"&"+_params;
		
		return _url;
	},
	//滚动到顶部
	goTopScroll:function(){
		document.getElementsByTagName('body')[0].scrollTop = 0;
	},
	//获取该期所有用户的参与记录
	getHistoryList:function(){
		
		var _this=this,
		start=this.option.count*this.option.limit,
		limit=this.option.limit;
		var customerId=$.util.getParam('customerId'),
//			periodId=$.util.getParamAsObject().periodId,	
			periodId=$('#perid').val(),
			params={
				"periodId":periodId,
				"start":start,
				"limit":limit
			};
			_url=_this.getUrl("json","allChips","getHistoryList",params);
			console.log(_url);
			
			//采用同步方式，避免第一次加载与滑到底部重复
			$.ajax({
				url:_url,
				type:"GET",
				dataType:"JSON",
				beforeSend:function(){
					 $.util.pageLoading.show();
				},
				success:function(data){
					 $.util.pageLoading.hide();
					console.log(data);
					if(data==null)
					{
//						alert('没有更多数据了')
						$('.gethState').show();
						_this.option.isAll=true;
					}
					else
					{
						//填充数据ui-history-db
						for(var i=0;i<data.length;i++)
						{
							
							var imgurl=data[i].headimgurl!=''&&data[i].headimgurl!=null?data[i].headimgurl:$('#defaultImg').val(),
								nickname=data[i].nickname!=''&&data[i].nickname!=null?data[i].nickname:'匿名用户';
							var li='<li>'
								+'<img src="'+imgurl+'"/>'
								+'<p><i class="ui-his-uname">'+nickname+'</i><br /><i class="c8">('+data[i].city+'IP:'+data[i].ip+')</i></p>'
								+'<p>参与了<i style="color:#ff8f12">'+data[i].count+'</i>人次<i class="c8"> '+data[i].updateTime+'</i></p>'
								'</li>';
							$('.ui-history-db').append(li);
						}
						_this.option.count++;
						if(data.length<limit)
						{
							$('.gethState').show();
							_this.option.isAll=true;
						}
						
					}
				}
			})
		
	},
	getOrderDetail:function(){
		var _this=this,
			fromOpenId=$('#fromOpenId').val(),              //分享人的openid
			periodId=$('#perid').val(),                     //期id
			pids=$.util.getParamAsObject().id;              //商品id
			
		var perSubmitOrder=function(count){
			var type="buy",
			params={
				"pids":pids,
				"counts":count,
				"type":type
			};
			params = $.extend(params,window.buyParams);
			odurl=_this.getUrl("json","product","preSubmitOrder",params);
			return odurl;
		}
		var createParRecord=function(oid,periodCount,count){
			
			var params={
				"periodId":periodId,
				"oid":oid,
				"pid":pids,
				"periodCount":periodCount,
				"count":count*periodCount,
				"fromOpenId":fromOpenId
			},
			_url=_this.getUrl("json","allChips","createParRecord",params);
			console.log('地址:'+_url)
			$.ajax({
				type:"GET",
				url:_url,
				dataType:"JSON",
				beforeSend:function(){
					 $.util.pageLoading.show();
				},
				success:function(data){
					$.util.pageLoading.hide();
					var p = {
                                "tplid": 1,
                                "id": oid
                    }
					var u=_this.getUrl("html","product&force=1","getOrderDetail",p);
					//成功 跳转订单详情页
					if (data && data.code==0) {
						window.location.href=u;
					}
				}
			})
				
		}
		
		$('#indBuySubmit').on('click',function(){
			if(!$(this).hasClass('disable'))
			{
				var periodCount=parseInt($('#periodCount').text()), //期次
				count=parseInt($('#pcount').text());            //人次
				
				var _url=perSubmitOrder(count*periodCount);
				$.ajax({
					type:"GET",
					url:_url,
					dataType:"JSON",
					beforeSend:function(){
						 $.util.pageLoading.show();
					},
					success:function(data){
						$.util.pageLoading.hide();
						//成功返回订单号
						if(data.id)
						{
							createParRecord(data.id,periodCount,count);
						}
					}
				});
			}
		});
			
	},
	checkBottom:function(){
		this.goTopScroll();
		var _this=this;
		var s=this.option.start,l=this.option.limit;
//		_this.getHistoryList();
		$(window).scroll(function(){
		　　var scrollTop = $(this).scrollTop();
		　　var scrollHeight = $(document).height();
		　　var windowHeight = $(this).height();
		　　if(scrollTop + windowHeight == scrollHeight){
			 if(!_this.option.isAll)
			 {
			 	_this.getHistoryList();
			 }　
		　　}
		});
		
	},
	uictrl:function(){
		
		var plus=$('.plus'),minus=$('.minus');
		//初始化
		
		var initMin=function(obj){
			obj.each(function(){
				var minCopies=parseInt(obj.attr('data-default'));
				$('#pcount').html(minCopies);
			})
			
		}($('.ui-plus'))
		
		var checkActive=function(Value,obj){
			var plus=obj.find('.plus'),
				minus=obj.find('.minus'),
				totalCopies=parseInt(obj.attr('data-total')),
				minCopies=parseInt(obj.attr('data-min'));
			console.log(Value+','+totalCopies);
			if(Value<=minCopies)
			{
				minus.removeClass('active');
				if(Value > (totalCopies-1))
				{
					plus.removeClass('active');
				}
				else
				{
					plus.addClass('active');
				}
				
			}
			else if(Value > (totalCopies-1))
			{
				plus.removeClass('active');
				minus.addClass('active');				
			}
			else
			{
				plus.addClass('active');
				minus.addClass('active');			
			}
			maskSum();
		}
		
		var init=function(){
			
			
			minus.on('click',function(){
				var pvalue=$(this).parent().find('.ui-pValue');
				var obj=$(this).parent();
				if($(this).hasClass('active'))
				{
					var value=parseInt(pvalue.html());
					value--;
					pvalue.html(value);
					checkActive(value,obj);
				}
				return false;
				
			});
			plus.on('click',function(){
				var pvalue=$(this).parent().find('.ui-pValue');
				var obj=$(this).parent();
				if($(this).hasClass('active'))
				{
					var value=parseInt(pvalue.html());
					value++;
					pvalue.html(value);
					checkActive(value,obj);
					
				}
				return false;
				
			});
			maskSum();
		}
		
		var maskSum=function(){
			var sum=0,persum=0,perMoney;
			perMoney=parseFloat($('.ui-menu-drsel').attr('data-pm'));
			$('.ui-pValue').each(function(index){
				
				
				
				if(index==0)
				{
					
					persum=persum+parseInt($(this).html());
				}
				else
				{
					sum=sum+(persum*parseInt($(this).html()));
				}
			});
			$('#totalPay').html((sum*perMoney)+'夺宝币');
			$('#totalPer').html(persum);
		}
		
		$('.ui-plus').each(function(){
			var preValue=parseInt($(this).find('.ui-pValue').html());
			checkActive(preValue,$(this));
		});
		init();
		
	},
	//前往最新一期
	goNewPeriod:function(){
		var params={
				"id":$.util.getParamAsObject().id
			}
		
		window.location.href=this.getUrl("html","product","getProduct",params);
	},
	//前往当前期的详情
	goActiveProduct:function(obj){
		
		var periodId=$(obj).attr('data-pid');
		var params={
				"id":$.util.getParamAsObject().pid,
				"periodId":periodId
			}
		
		window.location.href=this.getUrl("html","product","getProduct",params);
	},
	PayCtrl:function(_this,t,event){
	
			if(event)
			{
				var tar=event.target.className;
				if(tar!="mask")
				{
					return false;
				}
			}
			
			if(t&&!$(_this).hasClass('disableBtn'))
			{
				$('#payCtrl').show();
			}
			else
			{
				$('#payCtrl').hide();
			}
		
	},
	shareShow:function(){
		$('.shareMask').show();
	}
}
